package com.jbit.hxy.controller;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jbit.hxy.common.Result;
import com.jbit.hxy.mapper.DishFlavorMapper;
import com.jbit.hxy.mapper.WxDishFlavorMapper;
import com.jbit.hxy.model.dto.FlavorQueryDTO;
import com.jbit.hxy.model.po.WxDishFlavor;
import com.jbit.hxy.model.vo.WxDishFlavorVO;
import com.jbit.hxy.service.menu.WxDishFlavorService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/flavor")
@RequiredArgsConstructor
@Tag(name = "菜品口味管理", description = "菜品口味管理")
public class WxDishFlavorController {
    private final WxDishFlavorService wxDishFlavorService;
    private final WxDishFlavorMapper wxDishFlavorMapper;
    private final DishFlavorMapper dishFlavorMapper;
    @GetMapping("/list")
    @Operation(summary = "菜品口味列表", description = "菜品口味列表")
    public Result list() {
        return wxDishFlavorService.getFlavor();

    }
    @PostMapping("/save")
    @Operation(summary = "菜品口味保存", description = "菜品口味保存")
    public Result save(@RequestBody WxDishFlavor wxDishFlavor) {
        if (wxDishFlavor.getId() == null) {
            return Result.fail("菜品口味id不能为空");
        }
        wxDishFlavorService.save(wxDishFlavor);
        return Result.success("保存成功");

    }
    @PostMapping("/update")
    @Operation(summary = "菜品口味更新", description = "菜品口味更新")
    public Result update(@RequestBody WxDishFlavor wxDishFlavor) {
        if (wxDishFlavor.getId() == null) {
            return Result.fail("菜品口味id不能为空");
        }
        wxDishFlavorService.updateById(wxDishFlavor);
        return Result.success("更新成功");
    }
    @GetMapping("/delete")
    @Operation(summary = "菜品口味删除", description = "菜品口味删除")
    public Result delete(@RequestParam("id") Integer id) {
        if (id == null) {
            return Result.fail("菜品口味id不能为空");
        }
        wxDishFlavorService.removeById(id);
        return Result.success("删除成功");
    }
    @GetMapping("/getFlavorByDishId")
    @Operation(summary = "根据菜品id获取口味", description = "根据菜品id获取口味")
    public Result<List<WxDishFlavorVO>> getFlavorByDishId(@RequestParam(value = "dishId") Integer dishId){
        List<Integer> flavorIds = dishFlavorMapper.getFlavorIdByDishId(dishId);
        List<WxDishFlavorVO> wxDishFlavorVOS = new ArrayList<>();
        for (Integer flavorId : flavorIds) {
            WxDishFlavor wxDishFlavor = wxDishFlavorMapper.selectFlavorById(flavorId);
            String values = wxDishFlavor.getValues();
            Gson gson = new Gson();
            Type listType = new TypeToken<List<String>>() {}.getType();
            List<String> sugarLevels = gson.fromJson(values, listType);
            WxDishFlavorVO wxDishFlavorVO = new WxDishFlavorVO();
            BeanUtils.copyProperties(wxDishFlavor,wxDishFlavorVO);
            wxDishFlavorVO.setValues(sugarLevels);

            wxDishFlavorVOS.add(wxDishFlavorVO);


        }
        return Result.success(wxDishFlavorVOS);
    }
    @GetMapping("/getFlavorIdByDishId")
    @Operation(summary = "根据菜品id获取口味id", description = "根据菜品id获取口味id")
    public Result<List<Integer>> getFlavorIdByDishId(@RequestParam(value = "dishId") Integer dishId){
        List<Integer> flavorIds = dishFlavorMapper.getFlavorIdByDishId(dishId);
        return Result.success(flavorIds);

    }
    @GetMapping("/selectFlavorById")
    @Operation(summary = "根据口味id获取口味", description = "根据口味id获取口味")
    public Result<WxDishFlavor> selectFlavorById(@RequestParam(value = "id") Integer id){
        WxDishFlavor wxDishFlavor = wxDishFlavorMapper.selectFlavorById(id);
        return Result.success(wxDishFlavor);

    }

}
